package com.liukh.obwiki;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liukh.obwiki.entity.User;
import com.liukh.obwiki.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@SpringBootTest
class ObwikiApplicationTests {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void queryUser(){
        List<User> users = userMapper.selectList(new QueryWrapper<>());
        System.out.println(users);
    }

    @Test
    public void testInsert(){
        User user = new User();
        user.setLoginName("wj");
        user.setPassword("123456");
        user.setId(9999L);
        user.setName("Wuji");
        int result = userMapper.insert(user);
        System.out.println(result); //影响的行数
        System.out.println(user); //id自动回填
    }
    @Test
    public void testUpdateById(){
        User user = new User();
        user.setId(1L);
        user.setName("测试2");
        int result = userMapper.updateById(user);
        System.out.println(result);
    }

    @Test
    public void testSelectById(){
        User user = userMapper.selectById(1L);
        System.out.println(user);
    }

    @Test
    public void testSelectBatchIds(){
        List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
        users.forEach(System.out::println);
    }

    @Test
    public void testSelectByMap(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "测试2");
        map.put("login_name", "test");
        List<User> users = userMapper.selectByMap(map);
        users.forEach(System.out::println);
    }

    @Test
    public void testSelectPage() {
        Page<User> page = new Page<>(1,5);
        userMapper.selectPage(page, null);
        page.getRecords().forEach(System.out::println);//查询结果数据放入records属性中
        System.out.println(page.getCurrent());
        System.out.println(page.getPages());
        System.out.println(page.getSize());
        System.out.println(page.getTotal());
        System.out.println(page.hasNext());
        System.out.println(page.hasPrevious());
    }

    @Test
    public void testDeleteById(){
        int result = userMapper.deleteById(16L);
        System.out.println(result);
    }

    @Test
    public void testDeleteBatchIds() {
        int result = userMapper.deleteBatchIds(Arrays.asList(8, 9, 10));
        System.out.println(result);
    }

    @Test
    public void testDeleteByMap() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "test10");
        map.put("login_name", "test10");
        int result = userMapper.deleteByMap(map);
        System.out.println(result);
    }
}
